<template>
  <div class="channel-edit">
    <div class="my-channel">
      <div class="channel-header">
        <div class="channel-title">我的频道</div>
        <!-- 编辑按钮 -->
        <div class="channel-btn">
          <van-button plain hairline type="primary" round size="mini" @click="isEditshow=!isEditshow">{{ isEditshow ? '完成' : '编辑' }}</van-button>
        </div>
      </div>
      <div class="channel-body">
        <van-grid :gutter="10">
          <van-grid-item
          v-for="(obj,index) in channels"
          :key="index"
          @click="changeChannelClick(obj,index)"
          >
          <span slot="text" class="van-grid-item__text" :class="{ active: active === index}">{{ obj.name }}</span>
          <van-icon name="clear" slot="icon" class="van-icon" v-show="isEditshow && !flexChannel.includes(obj.id)"/>
          </van-grid-item>
        </van-grid>
      </div>
    </div>
    <div class="my-channel channel">
      <div class="channel-header">
        <div class="channel-title">频道推荐</div>
        <div class="channel-btn">
        </div>
      </div>
      <div class="channel-body">
        <van-grid :gutter="10">
          <van-grid-item
          dot
          v-for="(obj,index) in filterChannel"
          :key="index"
          :text="obj.name"
          @click="addChannel(obj)"
          >
          </van-grid-item>
        </van-grid>
      </div>
    </div>
  </div>
</template>
<script>
// 获取所有频道列表
import { getAllChannels, addMyChannel, deleteMyChannel } from '@/api/channel'
import { mapState } from 'vuex'
import { setItem } from '@/utils/stroage'
export default {
  data () {
    return {
      allChannel: [], // 全部频道列表
      isEditshow: false, // 控制频道右上角的小叉叉显示
      flexChannel: [0] // 固定推荐频道 不可以被删除
    }
  },
  props: ['channels', 'active'],
  methods: {
    // 获取全部频道列表函数
    async getAllChannels () {
      try {
        const { data } = await getAllChannels()
        this.allChannel = data.data.channels
      } catch (err) {
        console.log(err)
      }
    },
    // 点击将推荐频道添加到我的频道
    async addChannel (obj) {
      console.log(this.user)
      // 因为全部频道列表为计算属性 所以当问的频道列表发生变化后会重新计算
      if (this.user) {
        // 登录了
        try {
          await addMyChannel({
            channels: [{
              id: obj.id,
              seq: this.channels.length
            }]
          })
        } catch (err) {
          this.$toast('添加失败，请稍后再试')
        }
      } else {
        // 未登录
        this.channels.push(obj)
        setItem('toutiao-channel', this.channels)
      }
    },
    // 点击我的频道列表
    async changeChannelClick (obj, index) {
      // 判断在那种状态下被点击了
      if (this.isEditshow) {
        // 删除频道
        // 判断不可删除数据
        if (this.flexChannel.includes(obj.id)) {
          return false
        }
        if (index <= this.active) {
          // 让激活频道的索引减1
          this.$emit('updateActive', this.active - 1, true)
        }
        // 判断是否登录 然后删除
        if (this.user) {
          try {
            await deleteMyChannel(obj.id)
          } catch (err) {
            this.$toast('删除失败，请稍后再试')
          }
        } else {
          this.channels.splice(index, 1)
          setItem('toutiao-channel', this.channels)
        }
      } else {
        // 切换频道
        this.$emit('updateActive', index, false)
      }
    }

  },
  created () {
    // 获取全部频道列表
    this.getAllChannels()
  },
  computed: {
    ...mapState(['user']),
    // 筛选数组
    filterChannel: {
      get () {
        return this.allChannel.filter(item => {
          return !this.channels.find(obj => item.id === obj.id)
        })
      }
    }
  }
}
</script>
<style lang="less" scoped>
.channel-edit {
  box-sizing: border-box;
    padding-top: 85px;
    .active {
      color: coral;
    }

  .my-channel {
    display: flex;
    flex-direction: column;
    .channel-header {
      display: flex;
      margin-top: 20px;
      height: 28px;
      line-height: 28px;
      .channel-title {
        flex: 1;
        font-size: 36px;
        margin-left: 40px;
        color: #333;
      }
      .channel-btn {
        flex: 1;
        margin-right: 44px;
        text-align: right;
      }
    }
    .channel-body {
      margin-top: 60px;
      /deep/ .van-icon {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #ccc;
        z-index: 2
      }
      /deep/ .van-grid-item__icon-wrapper {
        position: unset;
      }
      .van-grid-item__icon+.van-grid-item__text {
        margin-top: 0;
      }
    }
  }
  .channel {
    margin-top: 44px;
  }
}
</style>
